package com.zx.tradeclient.ctrl.index1;

import java.util.List;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.NavUtils;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

import com.zx.base.ctrl.Constant;
import com.zx.base.util.ActivityTaskManager;
import com.zx.base.widget.QuitDialog;
import com.zx.tradeclient.R;
import com.zx.tradeclient.app.TradeApp;
import com.zx.tradeclient.ctrl.SearchActivity;
import com.zx.tradeclient.ctrl.index1.NavigationFragment.onSelectorNavigationListener;

public class Index1Activity extends FragmentActivity implements onSelectorNavigationListener, OnClickListener {

	private List<Navigation> navs;
	private NavigationFragment mNavigationFragment;
	private Button header_left, header_right;
	private TradeApp app;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.news);
		app = (TradeApp) getApplication();
		if (!(ActivityTaskManager.getInstance().containsName("Index1Activity"))) {
			ActivityTaskManager.getInstance().putActivity("Index1Activity", this);
		}

		mNavigationFragment = (NavigationFragment) getSupportFragmentManager().findFragmentById(R.id.fragment_navigation);
		navs = app.getNavs();
		mNavigationFragment.setNavs(navs);
		mNavigationFragment.setOnSelectorNavigationListener(Index1Activity.this);
		fillContent(navs.get(0));
		// getActionBar().setDisplayHomeAsUpEnabled(true);

		header_left = (Button) findViewById(R.id.header_left);
		header_left.setVisibility(View.GONE);
		header_right = (Button) findViewById(R.id.header_right);
		header_right.setBackgroundResource(R.drawable.header_dquery);
		header_right.setVisibility(View.VISIBLE);
		header_right.setOnClickListener(this);

	}

	public void onClick(View v) {
		if (v == header_left) {
		} else if (v == header_right) {
			Intent intent = new Intent(Index1Activity.this, SearchActivity.class);
			intent.putExtra("index", "1");
			startActivity(intent);
		}
	}

	@Override
	public void selector(Navigation navigation, int position) {
		fillContent(navigation);
	}

	private void fillContent(Navigation nav) {
		FragmentManager fragmentManager = getSupportFragmentManager();
		FragmentTransaction transaction = fragmentManager.beginTransaction();
		for (Navigation n : navs) {
			Fragment fragment = fragmentManager.findFragmentByTag(n.getId());
			if (fragment != null && !fragment.isDetached()) {
				transaction.detach(fragment);
			}
		}
		Fragment fragment = fragmentManager.findFragmentByTag(nav.getId());
		if (fragment == null) {
			fragment = NewsFragment.instance(nav);
			transaction.add(R.id.fl_content, fragment, nav.getId());
		} else {
			transaction.attach(fragment);
		}

		// transaction.replace(R.id.fl_content, fragment);
		// transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
		// transaction.addToBackStack(null);
		transaction.commit();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case 0:
			NavUtils.navigateUpFromSameTask(this);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			if (Constant.debug)
				Log.d("TMG", "ManageActivity KEYCODE_BACK");
			QuitDialog dialog = new QuitDialog(Index1Activity.this);
			dialog.exitConfirm();

			return true;
		}
		return super.onKeyDown(keyCode, event);
	}

}
